const connection = require("../../database")

/**
 * 用户服务-连接数据库
 */
class UserService {
   /**
   * 创建用户(连接数据库)
   * @param {Object} user
   * @param {String} user.name
   * @param {String} user.password
   * @returns {Promise<*>}
   */
   async create(user) {
    // 1.获取用户 user
    const {name, password} = user;

    // 2.拼接statement
    const statement = 'INSERT INTO `user` (name, password) VALUES (?, ?);'

    // 3.执行statement
    const [result] = await connection.execute(statement, [name, password]);

    return result
  }
  /**
   * 查询数据库中是否有该用户
   * @param {String} name
   * @returns {Promise<*>}
   */
  async getUserByName(name) {
    const statement = 'SELECT * FROM `user` WHERE name = ?;'
    const [result] = await connection.execute(statement, [name])
    return result
  }
}

module.exports = new UserService();